import {
  post
} from '@/assets/api/http'

const {
  SHA256
} = require('@/assets/lib/SHA256');

export const getSourceDetail = (data) => post('https://xzcsgpt.xzcs2022.com/source/getSourceDetail', data, {
  notoken: 1
})

// 消耗提问次数
export const gpt_ask_new = (data) => post('/api/user/gpt_ask_new', data)

// 查看提问次数
export const gpt_ask_show = (data) => post('/api/user/gpt_ask_show', data)


// 获取充值套餐
export const getComboList = (data) => post('/api/gptrecharge/getComboList', data)


// 获取充值套餐
export const goPay = (data) => post('/api/gptrecharge/pc_goPay', data)

// 添加GPT session
export const add_gpt_session = (data) => post('/api/user/add_gpt_session', data)
// 删除GPT session
export const del_gpt_session = (data) => post('/api/user/del_gpt_session', data)
// 查看GPT session
export const get_gpt_session = (data) => post('/api/user/get_gpt_session', data)

// GPT商务合作
export const gpt_business_cooperation_submit = (data) => post('/api/index/gpt_business_cooperation_submit', data)

export const ask = (data) => post('https://xzcsgpt.nlg.xzcs2022.com/nlg_v1_2/front/ask', data, {
  notoken: 1,
  is_GTP_TOKEN: 1
})

export const checkMessagePending = (data) => post('https://xzcsgpt.xzcs2022.com/chat/checkMessagePending', data, {
  notoken: 1
})

export const chatHistory = (data) => post('https://xzcsgpt.xzcs2022.com/chat/chatHistory', data, {
  notoken: 1,
  is_GTP_TOKEN: 1
})

export const stopMessageCompletion = (data) => post('https://xzcsgpt.xzcs2022.com/chat/stopMessageCompletion', data, {
  notoken: 1,
  is_GTP_TOKEN: 1
})

export const gpt_submit_evaluate = (data) => post('/api/user/gpt_submit_evaluate', data)

export const get_preface = (data) => post('/api/gptrecharge/get_preface', data)

export const getApiToken = function () {
  return new Promise(async function (resolve, reject) {
    var timestamp = Date.now();
    var params = {
      appKey: "2c7e5c9ac7edf0b0ac7865bbfe183a1a",
      timestamp: timestamp
    };
    var sign = 'appKey=' + "2c7e5c9ac7edf0b0ac7865bbfe183a1a" + ':appSecret=' + "b83d34a03c637a15eccaaf484cad9da0" + ':timestamp=' +
      timestamp;
    sign = SHA256(sign)
    var url = "https://xzcsgpt.xzcs2022.com/auth/getApiToken";
    let res = await post(url, params, {
      sign: sign,
      notoken: 1
    });
    if (res.errno == 0) {
      //   global.GTP_TOKEN = res.data.token
      localStorage.setItem('GTP_TOKEN', res.data.token)
      resolve(res.data.token)
    } else {
      resolve('')
    }
  })
}
